package com.mgq.java8.stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;

/**
 * <b>功能：</b>过滤<br>
 * <b>Copyright TCSL</b>
 * <li>版本&nbsp;&nbsp;&nbsp;&nbsp;修改日期&nbsp;&nbsp;&nbsp;&nbsp;部　　门&nbsp;&nbsp;&nbsp;&nbsp;作　者&nbsp;&nbsp;&nbsp;&nbsp;变更内容</li><hr>
 * <li>v1.0&nbsp;&nbsp;&nbsp;&nbsp;20220414&nbsp;&nbsp;营业后台&nbsp;&nbsp;&nbsp;&nbsp;马广奇&nbsp;&nbsp;&nbsp;&nbsp;创建类</li>
 * <br><br>
 *
 * @author 马广奇
 * @version 2022-04-14 20:13
 */
public class Filter {
    private static final Logger logger = LoggerFactory.getLogger(Filter.class);

    public static void main(String[] args) {
        anyMatch();
        allMatch();
        noneMatch();
        findAny();
        findFirst();
    }

    /**
     * 检查谓词是否至少匹配一个元素
     */
    public static void anyMatch() {
        boolean isVegetarian = StreamTest.menu.stream().anyMatch(Dish::isVegetarian);
        //是否至少有一个小于800
        boolean caloriesLess = StreamTest.menu.stream().anyMatch(dish -> dish.getCalories() < 800);
        System.out.println(isVegetarian);
        System.out.println(caloriesLess);
    }

    /**
     * 检查谓词是否匹配所有元素
     */
    public static void allMatch() {
        //是否都小于1000
        boolean caloriesLess = StreamTest.menu.stream().allMatch(dish -> dish.getCalories() < 1000);
        System.out.println(caloriesLess);
    }

    /**
     * 没有匹配
     */
    public static void noneMatch() {
        //没有素食
        boolean noneVegetarian = StreamTest.menu.stream().anyMatch(Dish::isVegetarian);
        System.out.println(noneVegetarian);
    }

    /**
     * 查找第一个
     */
    public static void findFirst() {
        //没有素食
        Optional<Dish> first = StreamTest.menu.stream().findFirst();
        first.ifPresent(System.out::println);
    }

    /**
     * findAny() 查找元素
     * isPresent()将在Optional包含值的时候返回true, 否则返回false
     */
    public static void findAny() {
        StreamTest.menu.stream().filter(dish -> dish.getCalories() > 1000)
                .findAny()
                //找到,打印,否则什么也不做
                .ifPresent(dish -> logger.info("找到{}", dish));
        //.orElseThrow(() -> new RuntimeException("没有找到素食者"));
    }
}
